﻿using System;
using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.Control;
using Super_Management_Mayhem.GameRoot;
using Super_Management_Mayhem.Screens.Menu.CreateAvatarColor;
using Super_Management_Mayhem.Screens.Menu.CreateAvatarName;
using Super_Management_Mayhem.Screens.Menu.SelectAvatar;
using Super_Management_Mayhem.Screens.Menu.SelectIcon;
using Super_Management_Mayhem.Screens.Menu.SelectTeams;
using Super_Management_Mayhem.Screens.Menu.Waiting;
using Super_Management_Mayhem.Screens.Playing.GameMode;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Input;
using Transformable_Engine_v2.Engine.Rendering;

namespace Super_Management_Mayhem.Screens.Menu
{
    public class MainMenu : Container
    {
        public delegate void OnReadyHandler();
        public event OnReadyHandler OnReady;

        private readonly int _id;
        private readonly InputKeys _input;

        private BaseMenuScreen<Menu> _currentMenu;
        private Menu _currentMenuName;

        public ProfileData Profile { get; private set; }

        public bool IsReady { get; private set; }

        private readonly int _teamCount;

        private readonly GameMode.GameModes _mode;

        public enum Menu
        {
            None,
            CreateNewAvatar_Name,
            CreateNewAvatar_Color,
            CreateNewAvatar_Icon,
            SelectAvatar,
            Waiting,
            SelectTeam,

        }

        public MainMenu(int _playerIndex,
                        GameMode.GameModes mode,
                        int teamCount)
        {
            _id = _playerIndex;

            _teamCount = teamCount;
            _mode = mode;

            _input = InputKeys.FromFile("Content/Config/Input/Player" + _id + ".input");

            KeyboardHandler.OnKeyboardPress += OnKeyPress;

            ChangeScreen(-1, Menu.SelectAvatar);
        }

        void ChangeScreen(int id, Menu nextScreen)
        {
            //The event is not targeted at this player
            if(id != _id && id != -1)
                return;

            //Save previous menu name
            switch(_currentMenuName)
            {
                case Menu.CreateNewAvatar_Name:
                    Profile.Name = (_currentMenu as AvatarNameMenu).CurrentName;
                    break;
                case Menu.CreateNewAvatar_Color:
                    Profile.Color = (_currentMenu as AvatarColorMenu).CurrentColor;
                    break;
                case Menu.CreateNewAvatar_Icon:
                    Profile.Icon = (_currentMenu as AvatarIconMenu).CurrentIcon;
                    break;
                case Menu.SelectAvatar:
                    Profile = (_currentMenu as SelectAvatarMenu).Profile ?? new ProfileData();
                    break;
            }

            if(_currentMenu != null)
            {
                _currentMenu.OnChangeScreen -= ChangeScreen;

                if(_currentMenuName != Menu.SelectTeam)
                    _currentMenu.Die();
                else
                    Children.Remove(_currentMenu);
                
            }
                
            switch(nextScreen)
            {
                case Menu.CreateNewAvatar_Name:
                    _currentMenu = new AvatarNameMenu(SuperManagementMayhem.HudSize, Profile.Name);
                    break;
                case Menu.CreateNewAvatar_Color:
                    _currentMenu = new AvatarColorMenu(SuperManagementMayhem.HudSize, Profile.Color);
                    break;
                case Menu.CreateNewAvatar_Icon:
                    _currentMenu = new AvatarIconMenu(SuperManagementMayhem.HudSize, Profile.Icon);
                    break;
                case Menu.Waiting:
                    if ((_mode == GameMode.GameModes.TeamDeathMatch || _mode == GameMode.GameModes.TeamMoneyMayhem) && _currentMenuName != Menu.SelectTeam)
                    {
                        ChangeScreen(-1, Menu.SelectTeam);
                        return;
                    }
                    else
                    {
                        Wait();
                    }
                    break;
                case Menu.SelectAvatar:
                    _currentMenu = new SelectAvatarMenu(SuperManagementMayhem.HudSize);
                    break;
                case Menu.SelectTeam:
                    _currentMenu = SelectTeams.SelectTeamsMenu.CreateInstance(SuperManagementMayhem.HudSize, _teamCount, SuperManagementMayhem.PlayerCount, _id);
                    break;
            }

            _currentMenu.OnChangeScreen += ChangeScreen;

            _currentMenuName = nextScreen;
            AddChildren(_currentMenu);
        }

        void Wait()
        {
            Profile.Save();
            Profile.Id = _id;

            if(SelectTeamsMenu.HasTeams)
                Profile.Team = SelectTeamsMenu.GetInstance().GetTeam(_id);
            
            _currentMenu = new WaitingMenu(SuperManagementMayhem.HudSize);
            IsReady = true;
            if (OnReady != null)
                OnReady();
        }

        private void OnKeyPress(KeyboardInput input)
        {
            if(input.KeyCode == _input.UpKey)
                _currentMenu.UpKey(_id);
            else if(input.KeyCode == _input.LeftKey)
                _currentMenu.LeftKey(_id);
            else if(input.KeyCode == _input.RightKey)
                _currentMenu.RightKey(_id);
            else if(input.KeyCode == _input.DownKey)
                _currentMenu.DownKey(_id);
            else if(input.KeyCode == _input.AttackKey)
                _currentMenu.AcceptKey(_id);
            else if(input.KeyCode == _input.ActionKey)
                _currentMenu.CancelKey(_id);
        }

        public override void Draw()
        {
            ViewHandler.SetView("viewMenu" + _id);
            base.Draw();
            ViewHandler.RevertToDefaultView();
        }

        public void EndWaiting()
        {
            (_currentMenu as WaitingMenu).EndWaiting();
        }
    }
}